package com.eagleframework.component.aop;

import com.eagle.util.log.LogLevel;
import com.eagle.util.log.LogWriter;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * 实现AOP日志输出
 *
 * <p>需要开启AOP{@link EnableAspectJAutoProxy}
 * 在需要被调用时输出日志的方法上加{@link AopLog}
 * 注解可实现输出指定日志
 *
 * @author yaojianxing
 */
@Aspect
public class AopLogAspect {
    /**
     * 当方法上有{@link AopLog}注解时执行该切面
     *
     * @param pjp    切入点
     * @param aopLog 指定注解
     */
    @Before("@annotation(aopLog)")
    public void execute(AopLog aopLog) {
        LogLevel level = aopLog.level();
        if (LogLevel.INFO.equals(level)) {
            LogWriter.info(aopLog.value());
        } else if (LogLevel.WARN.equals(level)) {
            LogWriter.warn(aopLog.value());
        } else if (LogLevel.DEBUG.equals(level)) {
            LogWriter.debug(aopLog.value());
        } else if (LogLevel.ERROR.equals(level)) {
            LogWriter.error(aopLog.value());
        }
    }
}